package com.ysu.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysu.shop.pojo.Category;
import com.ysu.shop.mapper.CategoryMapper;
import com.ysu.shop.pojo.Permission;
import com.ysu.shop.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 霍京
 * @since 2021-3-03
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Override
    public Object listByTree() {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", 0);
        List<Category> list = this.list(wrapper);
        for (Category i : list) {
            getChildren(i);
        }
        return list;
    }

    /**
     * 参数为父权限对象 通过父权限获取子权限, 这里假设为2级
     *
     */
    private void getChildren(Category category) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", category.getId());
        // List<Permission> list =this.list(wrapper) ;
        // for (Permission i : list) {
        //   if (permission.getType() != 0) {
        //     getChildren(permission);
        //   }
        // }
        category.setChildren(this.list(wrapper));
    }
}
